TOP

SQL Aliases

YouLibreCalc for Excel logo

SQL Aliases Description

SQL aliases are used to give a table or column in a table a temporary name.

Aliases are often used to make column names more readable.

An alias exists only for the duration of the current request.

An alias is created using the AS keyword.


Column Alias ​​Syntax

SELECT column_name AS alias_name
FROM table_name

Alias ​​Table Syntax

SELECT column_name(s)
FROM table_name AS alias_name

Demonstration database

Below is a sample from the table "Customers" ("Customers"):

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico

And the sample from the table "Orders" ("Orders"):

ProductIDOrderIDCustomerIDEmployeeIDOrderDateShipperID
1102489051996-07-043
2102498161996-07-051
3102503441996-07-082

Examples of aliases for columns

The following SQL statement creates two aliases, one for the CustomerID column and one for the CustomerName column:

Run SQLSELECT CustomerID AS ID, CustomerName AS Customer 
FROM Customers

The following SQL statement creates two aliases: one for the CustomerName column and one for the ContactName column.

Note: It requires double quotes or square brackets if the alias contains spaces:

Run SQLSELECT CustomerName AS Customer, ContactName AS 'Contact Person' 
FROM Customers

The following SQL statement creates an alias named "Address" ("Address") that concatenates four columns (address, zip code, city, and country):

SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address 
FROM Customers

To make the above SQL statement work in MySQL, use the following:

Run SQLSELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address 
FROM Customers

To make the above SQL statement work in Oracle, use the following:

SELECT CustomerName, (Address || ', ' || PostalCode || ' ' || City || ', ' || Country) AS Address 
FROM Customers

An example of an alias for tables

The following SQL statement selects all orders from the customer with CustomerID = 4 ("Around the Horn"). We use the tables "Customers" and "Orders" and give them table aliases "c" and "o" respectively (here we use aliases to make the SQL query shorter):

Run SQLSELECT o.OrderID, o.OrderDate, c.CustomerName 
FROM Customers AS c, Orders AS o 
WHERE c.CustomerName = 'Around the Horn' AND c.CustomerID = o.CustomerID

The following SQL statement is the same as above, but without the aliases:

Run SQLSELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName = 'Around the Horn' AND Customers.CustomerID = Orders.CustomerID

Aliases can be useful when:

  • More than one table is involved in the query;
  • The query uses functions;
  • Column names are large or illegible;
  • Two or more columns are joined together.